package com.nand.module.news.ui;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.Callback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.module.news.R;
import com.nand.module.news.bean.NewsItemBean;
import com.nand.module.news.constant.URL;

import java.util.List;

@Route(path = "/news/details")
public class NewsDetailsActivity extends BaseActivity {

    private TextView tvTitle;
    private TextView tvDate;
    private TextView tvContenr;
    private ImageView ivImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean isActionBar() {
        return true;
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_news_details;
    }

    @Override
    protected void initView() {
        mActionBar().setCenterText("新闻标题");
        tvTitle = findViewById(R.id.tv_title);
        tvDate = findViewById(R.id.tv_date);
        tvContenr = findViewById(R.id.tv_content);
        ivImage = findViewById(R.id.iv_image);
    }

    @Override
    protected void initData() {
        String newsId = getIntent().getExtras().getString("newsId");

        HttpParams params = new HttpParams();
        params.put("oid", newsId);
        OkGoUtils.getInstance().postByOkGo(this, URL.NEWS_ITEM_URL, params, ResponseBean.class, new Callback() {
            @Override
            public void onSuccess(Object response, int id) {
                ResponseBean data = (ResponseBean) response;
                NewsItemBean listBeans = JSON.parseObject(data.getDataInfo().toString(), NewsItemBean.class);
                List<NewsItemBean.NewsListBean> item = listBeans.getNewsList();
                tvTitle.setText(item.get(0).getTitle());
                tvDate.setText(item.get(0).getReleaseDate());
                tvContenr.setText(item.get(0).getContent());
                Glide.with(mContext).load(item.get(0).getNewsUrl()).into(ivImage);
            }

            @Override
            public void onError(Throwable e, int id) {
                Log.e("TAG","data:"+e.getMessage());
            }
        }, 0);
    }
}
